package com.test2015.Swing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ScriptParser {
	static HashMap<String,String> map = new HashMap<String,String>();
	static{
		map.put("name", "Guo");
		map.put("age", "22");
		map.put("gender", "male");
	}
	
	public static List<String> lineList = new ArrayList<String>(); 
	
	public static String parseLine(String line){
		StringBuffer res = new StringBuffer();
		StringBuffer key = new StringBuffer();
		
		int length = line.length();
		boolean openFlag = false;
		for(int i=0;i<length;i++){
			char tempChar = line.charAt(i);
			if(tempChar=='$' && line.charAt(i+1)=='{'){
				openFlag = true;
				i++;
			}else if(tempChar =='}'){
				openFlag = false;
				res.append(map.get(key.toString()));
				key.delete(0, key.length());
			}else if(openFlag){
				key.append(tempChar);
			}else{
				res.append(tempChar);
			}
			
		}
		
		return res.toString();
	}
	
	public static void loadScript(String sname) throws Exception {
		File scriptFile = new File("script/" + sname);
		BufferedReader br = new BufferedReader
				(new InputStreamReader(new FileInputStream(scriptFile)));
		String line = null;
		while ((line = br.readLine()) != null) {
			lineList.add(parseLine(line));
		}
		br.close();
	}
	
	public static void main(String[] args) throws Exception {
		loadScript("test.sc");
		System.out.println(lineList.get(0));
		System.out.println(lineList.get(1));
		System.out.println(lineList.get(2));
	}
}
